const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
	@property
	speed: number = 300;
	inputList: Map<number, boolean> = new Map();



	onLoad() {
		cc.systemEvent.on(
			cc.SystemEvent.EventType.KEY_DOWN,
			this.onKeyDown,
			this
		);
		cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
	}

	start() {}

	onKeyDown(e: cc.Event.EventKeyboard) {
		this.inputList[e.keyCode] = true;
		// log(e.keyCode + ":" + true);
	}

	onKeyUp(e: cc.Event.EventKeyboard) {
		this.inputList[e.keyCode] = false;
		// log(e.keyCode + ":" + false);
	}

	update(dt) {
		this.onMove();
	}

	onMove() {
		let v: cc.Vec2 = cc.v2();
		let rigidBody = this.node.getComponent(cc.RigidBody);
		// v = rigidBody.linearVelocity;
		v = cc.v2(0, v.y);

		if (this.inputList[cc.macro.KEY.up] || this.inputList[cc.macro.KEY.w]) {
			v.y = this.speed;
		}
		if (
			this.inputList[cc.macro.KEY.down] ||
			this.inputList[cc.macro.KEY.s]
		) {
			v.y = -this.speed;
		}
		if (
			this.inputList[cc.macro.KEY.left] ||
			this.inputList[cc.macro.KEY.a]
		) {
			v.x = -this.speed;
		}
		if (
			this.inputList[cc.macro.KEY.right] ||
			this.inputList[cc.macro.KEY.d]
		) {
			v.x = this.speed;
		}

		if (
			this.inputList[cc.macro.KEY.right] ||
			this.inputList[cc.macro.KEY.d]
		) {
			v.x = this.speed;
		}
		rigidBody.linearVelocity = v;
	}
}
